package com.ilovepostcard;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

public class SearchActivity extends BaseActivity implements OnClickListener{
	
	
	boolean searchByUser= true;
	TextView searchUser;
	TextView searchPC;
	Button confirm;
	int selectedColor;
	int unselectedColor;
	View mTagsView;
	EditText mInput;
	GridView mTags;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 
		init();
	}
	
	
	private void init()
	{
		searchUser = (TextView)findViewById(R.id.searchUser);
		searchPC= (TextView)findViewById(R.id.searchPostcard);
		confirm = (Button)findViewById(R.id.confirm);
		searchUser.setOnClickListener(this);
		searchPC.setOnClickListener(this);
		confirm.setOnClickListener(this);
		setSearchFlag(true);
		
		selectedColor = this.getResources().getColor(R.color.color_gray_1);
		unselectedColor = this.getResources().getColor(R.color.color_title);
		
		mInput =  (EditText)findViewById(R.id.search_input);
		
		
		mTagsView = findViewById(R.id.search_tags);
		mTags = (GridView)findViewById(R.id.tags);
		mTags.setAdapter(new TagAdpater());
		mTags.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				mInput.setText((String)mTags.getItemAtPosition(arg2));
			}
			
		});
		mTagsView.setVisibility(View.GONE);
	}
	
	private void setSearchFlag(boolean toUser)
	{
		if(toUser == searchByUser) return;
		
		if(toUser)
		{
			searchUser.setTextColor(selectedColor);
			searchPC.setTextColor(unselectedColor);
		}
		else 
		{
			searchUser.setTextColor(unselectedColor);
			searchPC.setTextColor(selectedColor);
		}

		searchByUser = toUser;
	}
	
	class TagAdpater extends BaseAdapter
	{

		String[] tags = getResources().getStringArray(R.array.tags);
		@Override
		public int getCount() {
			
			return tags.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return tags[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView title= null;
			if(convertView == null)
			{
				convertView= LayoutInflater.from(SearchActivity.this).inflate(R.layout.search_tags_item, null);
				AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,AbsListView.LayoutParams.WRAP_CONTENT);
				params.height= 60;
				convertView.setLayoutParams(params);
			}
			
			title = (TextView)convertView.findViewById(R.id.share_tag);
			title.setText(tags[position]);
			return convertView;
		}
		
	}



	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.searchUser)
		{
			mTagsView.setVisibility(View.GONE);
			setSearchFlag(true);
		}
		else if(v.getId() ==R.id.searchPostcard)
		{
			mTagsView.setVisibility(View.VISIBLE);
			setSearchFlag(false);
		}
		else if(v.getId() == R.id.confirm)
		{
			String key = null;
			if(mInput.getEditableText()!= null)
				key = mInput.getEditableText().toString();
			if(searchByUser)
			{
			
				if(key!=null)
				{

					Intent nextPage = new Intent(this,SearchResultUserActivtiy.class);
					nextPage.putExtra("key", key);
					startActivity(nextPage);
				}
			}
			else
			{
				if(key != null)
				{
					Intent nextPage = new Intent(this,SearchResultPostCardActivity.class);
					nextPage.putExtra("key", key);
					startActivity(nextPage);
				}
				
			}
		}
		
	}

}
